<?php if(!defined('IN_APP')) exit('No direct script access allowed');

function __autoload($className)
{
    $className = str_replace('_controller','',$className);
    $paths = array(
        APP => array(
            'components' => COMPONENTS_EXT . '.' . EXT,
            'controllers' => CONTROLLERS_EXT . '.' . EXT,
            'helpers' => HELPERS_EXT . '.' . EXT,
            'models' => MODELS_EXT . '.' . EXT,
            'views' => VIEWS_EXT . '.' . EXT,
            'widgets' => WIDGETS_EXT . '.' . EXT
        ),
        CORE => array(
            'helpers' => HELPERS_EXT . '.' . EXT,
            '' => EXT
        )
    );
    foreach($paths as $path => $folders)
    {
        foreach($folders as $folder => $ext)
        {
            $filename = ($folder != '') ?
                ROOT . DS . $path . DS . $folder . DS . strtolower($className) . '.' . $ext :
                ROOT . DS . $path . DS . strtolower($className) . '.' . $ext;
            if(file_exists($filename))
            {
                require_once $filename;
            }
        }
    }
}
/**
 * Environment Class
 * 
 * Used to control all config variables and handle
 * bootstrapping for the application.
 */
class Environment
{
    private $defines = array(
        // Paths
        'APP' => 'application',
        'CORE' => 'core',
        // Extensions
        'EXT' => 'php',
        'COMPONENTS_EXT' => 'components',
        'CONTROLLERS_EXT' => 'controller',
        'HELPERS_EXT' => 'helpers',
        'MODELS_EXT' => 'model',
        'VIEWS_EXT' => 'view',
        'WIDGETS_EXT' => 'widgets',
        // Defaults
        'DEFAULT_CONTROLLER' => 'home',
        'DEFAULT_ACTION' => 'index',
        // Database 
        'DB_INTERFACE' => 'mysql',
        'DB_USER' => 'root',
        'DB_PASS' => '',
        'DB_HOST' => 'localhost',
        'DB_NAME' => 'valkyrie'
    );
    private $helpers = array(
        'debug'
    );
    public $db;
    
    public function __construct($env)
    {
        $this->_loadDefines();
        $this->_loadHelpers();
        $this->_setReporting($env);
        $this->_removeMagicQuotes();
        $this->_unregisterGlobals();
    }
    
    public function bootstrap($url)
    {
        $urlArray = array();
        $urlArray = explode('/',$url);
        
        $controller = ($urlArray[0] != '') ? $urlArray[0] : DEFAULT_CONTROLLER;
        array_shift($urlArray);
        $action = ($urlArray[0] != '') ? $urlArray[0] : DEFAULT_ACTION;
        array_shift($urlArray);
        $args = $urlArray;
        
        $controller .= '_controller';
        $controller = new $controller;
        if(method_exists($controller, $action))
        {
            $call = array( $controller , $action );
            call_user_func_array($call, $args);
        }
    }
    
    
    private function _loadHelpers()
    {
        foreach($this->helpers as $helper)
        {
            $filename = ROOT . DS . CORE . DS . 'helpers' . DS . strtolower($helper) . '.helper.' . EXT;
            if(file_exists($filename))
            {
                require_once $filename;
            }
        }
    }
    
    private function _loadDefines()
    {
        foreach($this->defines as $define => $value)
        {
            if(!defined($define))
                define($define, $value);
        }
    }
    
    private function _unregisterGlobals()
    {
        if(ini_get('register_globals'))
        {
            $array = array('_SESSION','_POST','_GET','_COOKIE','_REQUEST','_SERVER','_ENV','_FILES');
            foreach($array as $value)
            {
                foreach($GLOBALS[$value] as $key => $value)
                {
                    if($value === $GLOBALS[$key])
                    {
                        unset($GLOBALS[$key]);
                    }
                }
            }
        }
    }
    
    private function _removeMagicQuotes()
    {
        if(get_magic_quotes_gpc())
        {
            $_GET = $this->_stripSlashes($_GET);
            $_POST = $this->_stripSlashes($_POST);
            $_COOKIE = $this->_stripSlashes($_COOKIE);
        }
    }
    
    private function _setReporting($evn)
    {
        switch($env)
        {
            case 'dev' :
                error_reporting(E_ALL);
                ini_set('display_errors', 'On');
                break;
            case 'prod' :
                error_reporting(E_ALL);
                ini_set('display_errors', 'Off');
                ini_set('log_errors','On');
                ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error.log');
                break;
        }
        return;
    }
    
    private function _stripSlashes($value)
    {
        return is_array($value) ? array_map('$this->_stripSlashes', $value) : $this->_stripSlashes($value);
    }
}




/* EOF: Environment.php */